package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysUserDaoI;
import sy.model.SysUser;

@Repository("sysUserDao")
public class SysUserDaoImpl implements SysUserDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysUser sysUser) {
		return getCurrentSession().save(sysUser);//直接用
		
	}

	@Override
	public void remove(SysUser sysUser) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysUser);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysUserDaoI#update(sy.model.SysUser)
	 */
	@Override
	public void update(SysUser sysUser) {
		// TODO Auto-generated method stub
		getCurrentSession().update(sysUser);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysUserDaoI#list()
	 */
	@Override
	public List<SysUser> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysUser").list();
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysUserDaoI#getSysUserById(java.lang.String)
	 */
	@Override
	public SysUser getSysUserById(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysUser where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysUser> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysUserDaoI#listSysUserByKey(java.lang.String)
	 */
	@Override
	public List<SysUser> listSysUserByKey(String key) {
		// TODO Auto-generated method stub
		String hql = "from SysUser where code like :key or name like :key or telephone like :key or house like :key or identity like :key or sysAccount.id like :key";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		List<SysUser> list = query.list();
		return list;
	}

	@Override
	public List<SysUser> listSysUserByOwnerId(String ownerId) {
		// TODO Auto-generated method stub
		String hql = "SELECT b.id id,b.name name,b.code code,b.telephone telephone,b.house house,b.identity identity FROM sys_owner_renter_mapping a "+
		    "left join sys_user b on a.renter = b.account where a.owner = :ownerId";
		SQLQuery query = this.getCurrentSession().createSQLQuery(hql);
		query.setString("ownerId", ownerId);
		query.addScalar("id")
	    .addScalar("code")
	    .addScalar("name")
	    .addScalar("telephone")
	    .addScalar("house")
	    .addScalar("identity")
	    .setResultTransformer(Transformers.aliasToBean(SysUser.class));
		List<SysUser> list = query.list();
		return list;
	}

	@Override
	public SysUser getSysUserByAccount(String accountId) {
		// TODO Auto-generated method stub
		String hql = "from SysUser where sysAccount.id = :accountId";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("accountId", accountId);
		List<SysUser> list = query.list();
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public SysUser getSysUserByName(String name) {
		// TODO Auto-generated method stub
		String hql = "from SysUser where name = :name";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("name", name);
		List<SysUser> list = query.list();
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<SysUser> listSysUserByAccountIds(List<String> accountIds) {
		// TODO Auto-generated method stub
		String hql = "from SysUser where sysAccount.id in :accountIds";
		Query query = getCurrentSession().createQuery(hql);
		query.setParameterList("accountIds", accountIds);
		List<SysUser> list = query.list();
		return list;
	}

}
